//
//  YRYModel.m
//  YGMobile
//
//  Created by zhanglu on 16/10/8.
//  Copyright © 2016年 Elephants Financial Service. All rights reserved.
//

#import "YRYModel.h"

@implementation YRYModel

- (NSDictionary *)dictionaryValue {
    NSDictionary *originalDictValue = [super dictionaryValue];
    NSMutableDictionary *dictValue = [originalDictValue mutableCopy];
    for (NSString *key in originalDictValue) {
        if ([self valueForKey:key] == nil) {
            [dictValue removeObjectForKey:key];
        }
    }
    return [dictValue copy];
}

+ (MTLValueTransformer *)MTTimestampToDateTransformer
{
    return [MTLValueTransformer reversibleTransformerWithForwardBlock:^(NSNumber *timestamp) {
        return [NSDate dateWithTimeIntervalSince1970: timestamp.longLongValue / 1000];
    } reverseBlock:^(NSDate *date) {
        return [date timeIntervalSince1970InMillionSecond];
    }];
}

- (nonnull NSDictionary *)jsonValue
{
    return [MTLJSONAdapter JSONDictionaryFromModel:self];
}

- (long long)longTransformWithDate:(NSNumber *)date
{
    BOOL isMilli = [self isMilliSecondWithDateNumber:date];
    long long dateInterval = date.longLongValue;
    return isMilli ? (dateInterval / 1000) : dateInterval;
}

- (BOOL)isMilliSecondWithDateNumber:(NSNumber *)date
{
    NSString *dateStr = [NSString stringWithFormat:@"%@",date];
    NSUInteger length = dateStr.length;
    
    if (length == 13) {//是毫秒
        return YES;
    }
    else {
        return NO;
    }
}


@end
